package com.qf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

/*
 *@Description:
 *@author:刘哥
 *@Date:14:35 2022/7/14
 */
@RestController
@RequestMapping("/file")
public class FileController {
    @RequestMapping("/upload")
    public String upload(String name, MultipartFile file, HttpSession session){
        try {
            String basepath = session.getServletContext().getRealPath("/upload");
            File dirs = new File(basepath);
            if(!dirs.exists()){
                dirs.mkdirs();
            }
            String filename = file.getOriginalFilename();//获取上传组传的name值
            file.transferTo(new File(basepath+File.separator+filename));
            return "success";
        } catch (IOException e) {
            e.printStackTrace();
            return "fail";
        }


    }
}
